{% extends '_base_list.html' %}

{% block title %}
    权限菜单
{% endblock %}


{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='plugins/jquery-treegrid/css/jquery.treegrid.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">

    <style type="text/css">
        #privilegeTable tbody > tr > td:nth-of-type(2){
            padding-left: 25px;
            text-align: left !important;
        }
        #privilegeTable thead > tr > th:nth-of-type(2){
            padding-left: 60px;
            text-align: left !important;
        }
    </style>

{% endblock %}


<!-- Content head -->
{% block head_title %}
    系统管理
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">权限菜单</li>
    </ol>
{% endblock %}


<!-- Content body -->
{% block title_filter %}
{% endblock %}

{% block body_operator %}
    <div class="operator mar-btm">
        <div class="row">
            <div class="col-xs-12 text-center">
                {% if current_user.has_perm('perms-permission-create') %}
                    <button class="btn btn-primary btn-sm" id="add"><i class=""></i> 新增权限</button>
                {% endif %}
                {% if current_user.has_perm('perms-permission-update') %}
                    <button class="btn btn-mint btn-sm" id="edit"><i class=""></i> 修改权限</button>
                {% endif %}
                {% if current_user.has_perm('perms-permission-delete') %}
                    <button class="btn btn-danger btn-sm" id="del"><i class=""></i> 删除权限</button>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block body_table %}
    <table id="privilegeTable" class="table table-auto">
    </table>
{% endblock %}


{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>
    <!--jquery-treegrid-->
    <script src="{{ url_for('static', filename='plugins/jquery-treegrid/js/jquery.treegrid.min.js') }}"></script>
    <!--bootstrap-table-->
    <script src="{{ url_for('static', filename='plugins/bootstrap-table/extensions/treegrid/bootstrap-table-treegrid.min.js') }}"></script>

    <script type="text/javascript">

        $(function () {

            // 初始化表格
            var $tableEle = $("#privilegeTable");
            var options = {
                columns: [
                    {title: '选择', field: 'selected', radio: true},
                    {title: '名称', field: 'name', sortable: false},
                    {title: '模块', field: 'module', sortable: false},
                    {title: '控制器', field: 'controller', sortable: false},
                    {title: '请求类型', field: 'action', sortable: false},
                    {title: '图标', field: 'icon', sortable: false},
                    {title: '是否为菜单', field: 'is_menu', sortable: false,
                        formatter: function (value, row, index, field) {
                            // value: 字段值; row: 行记录数据; index: 行索引 ;field: 行字段
                            if(value === 0){
                                return '<span class="text text-danger text-bold">否</span>'
                            }else{
                                return '<span class="text text-success text-bold">是</span>'
                            }
                        }
                    },
                    {title: '排序', field: 'sort', sortable: false},
                    {title: '创建时间', field: 'create_time',sortable: false}
                ],
                url:'{{ url_for("perms.api-permission-list") }}',
                sortable: false,
                pagination: false,


                // bootstrap-table-treegrid.js 插件配置 -- start
                //在哪一列展开树形
                treeShowField: 'name',
                //指定父id列
                parentIdField: 'pid',

                // 初始化treegrid表格数据
                onResetView: function() {
                    var columns = $tableEle.bootstrapTable('getOptions').columns;
                    if (columns && columns[0][1].visible) {     // columns[0][1] --> name(field)
                        $tableEle.treegrid({
                            initialState: 'collapsed',      // 所有节点都折叠
                            //initialState: 'expanded',    // 所有节点都展开
                            treeColumn: 1,
                            expanderExpandedClass: 'fa fa-minus',  //图标样式
                            expanderCollapsedClass: 'fa fa-plus'
                        });

                        //展开树形的第一级节点
                        if($tableEle.treegrid('getRootNodes').length){
                            $tableEle.treegrid('getRootNodes').treegrid('expand');

                            //展开树形的第二级节点
                            $.each($tableEle.treegrid('getRootNodes'), function (idx, val) {
                                console.log($(val));
                                $(val).treegrid('getChildNodes').treegrid('expand');
                            })
                        }
                    }
                }
                // bootstrap-table-treetreegrid.js 插件配置 -- end
            };
            var table = $tableEle.beesTable(options);

            // 操作
            bees.bindTableOperator({
                table: $tableEle,
                operator: {
                    add: {
                        href: '{{ url_for('perms.permission-create') }}',
                        isTransferParams: true
                    },
                    show: {
                        disable: true
                    },
                    edit: {
                        href: '{{ url_for('perms.permission-update', pk=default_pk) }}'
                    },
                    del: {
                        confirmCallback: function (data) {
                            setAjaxCSRFToken();
                            $.ajax({
                                type: 'post',
                                url: '{{ url_for("perms.permission-delete", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                                data : {},
                                success: function (result) {
                                    if (result.code != 200) {
                                        bees.tipError({
                                            message: result.data.message
                                        });
                                    }else {
                                       bees.tipSuccess({
                                            message: "删除成功"
                                       });

                                       //reload
                                       table.bt.bootstrapTable('refresh');
                                    }
                                }
                            })
                        }
                    }
                }
            })

        })
    </script>
{% endblock %}
